﻿/// <reference path="MyString.js" />

//**************************************包含获取网站的相关属性的方法***************************
var MyWebsite = {
    getServerAddr: function () {
        /// <summary>获取服务器的Url；返回的字符串最后包含斜杠符号“/”</summary>
        return window.location.protocol + "//" + this.getHost() + "/";
    },
    getHost: function () {
        /// <summary>获取Web服务器的IP:Port, 如: http://127.0.0.1:8225/
        ///在安全连接的情况下，某些时候host中会自动出现443端口，而实际是80端口</summary>
        var sHost = window.location.host;
        if (!sHost) {
            sHost = top.location.host;
        }
        if (sHost.indexOf(":443") >= 0)
            sHost = sHost.replace(":443", "");
        return sHost;
    },
    getQueryParam: function (sName, sQueryString) {
        /// <summary>获取QueryString的值</summary>
        /// <param name="sName" type="String">对应的键</param>
        /// <param name="sQueryString" type="String">不传入则默认获取当前页面的查询字符串的值</param>
        var match = RegExp(sName + '=([^&]*)').exec(sQueryString || location.search);
        return match && decodeURIComponent(match[1]);
    },
    encodeUrlParam: function (sData) {
        /// <summary>对url参数进行编码</summary>
        /// <param name="sData" type="String">需要编码的数据</param>
        return encodeURIComponent(sData);
    },
    encodeUrl: function (sUrl) {
        /// <summary>对url进行编码，直接传入url，方法会自动编码所有的url参数</summary>
        /// <param name="sUrl" type="String">指定需要编码的url</param>
        if (sUrl.indexOf("?") < 0) return sUrl;
        var sBasicUrl = MyString.leftOfLast(sUrl, "?");
        var sParams = MyString.rightOfLast(sUrl, "?");
        var aParams = sParams.split("&");
        var sEncodeParams = "";
        for (var i = 0; i < aParams.length; i++) {
            if (i > 0) {
                sEncodeParams += "&";
            }
            var aTempParam = aParams[i].split("=");
            sEncodeParams += aTempParam[0] + "=" + encodeURIComponent(aTempParam[1]);
        }
        return sBasicUrl + "?" + sEncodeParams;
    },
    parseToUrl: function (sVirtualPath) {
        /// <summary>把相对路径转换成完整的url地址</summary>
        /// <param name="sVirtualPath" type="String">传入的相对路径，只支持相对网站的根目录符号'~'</param>
        if (sVirtualPath.indexOf("~") != 0) return sVirtualPath;
        else {
            sVirtualPath = MyString.replaceAll(sVirtualPath, "\\", "/");
            return sVirtualPath.replace("~/", this.getServerAddr());
        }
    }
};

